今天要介紹如何使用conic-gradient
和rotate
模擬雷達掃描的動畫效果
<div class="radar">
<div class="scanner"></div>
<div class="grid"></div>
<div class="target target1"></div>
<div class="target target2"></div>
</div>
將內容居中排列
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #000;
}
創建雷達的外觀,包括圓形和淡出效果
.radar {
position: relative;
width: 300px;
height: 300px;
border-radius: 50%;
background: radial-gradient(circle, rgba(0, 255, 0, 0.1), transparent 70%);
border: 2px solid green;
box-shadow: 0 0 20px rgba(0, 255, 0, 0.6);
overflow: hidden;
}
創建同心圓和輻射線
.grid {
position: absolute;
width: 100%;
height: 100%;
background-image: radial-gradient(circle, transparent 30%, rgba(0, 255, 0, 0.3) 31%, transparent 32%),
radial-gradient(circle, transparent 60%, rgba(0, 255, 0, 0.3) 61%, transparent 62%),
radial-gradient(circle, transparent 90%, rgba(0, 255, 0, 0.3) 91%, transparent 92%),
linear-gradient(rgba(0, 255, 0, 0.3), rgba(0, 255, 0, 0.3)),
linear-gradient(90deg, rgba(0, 255, 0, 0.3), rgba(0, 255, 0, 0.3));
background-size: 100% 100%;
border-radius: 50%;
}
掃描線使用 conic-gradient
來模擬扇形效果,並通過旋轉動畫來呈現掃描
.scanner {
width: 100%;
height: 100%;
background: conic-gradient(from 90deg, rgba(0, 255, 0, 0.5) 10%, transparent 40%);
border-radius: 50%;
animation: scan 3s linear infinite;
position: absolute;
top: 0;
left: 0;
}
雷達上閃爍的目標點
.target {
position: absolute;
width: 8px;
height: 8px;
border-radius: 50%;
background-color: rgba(0, 255, 0, 0.8);
box-shadow: 0 0 10px rgba(0, 255, 0, 0.8);
}
掃描動畫和目標點的閃爍效果
.target1 {
top: 30%;
left: 60%;
animation: blink 1.5s infinite;
}
.target2 {
top: 70%;
left: 40%;
animation: blink 1.5s infinite 0.5s;
}
@keyframes scan {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
@keyframes blink {
0%, 100% {
opacity: 0;
}
50% {
opacity: 1;
}
}